<template>
  <div class="admin-login">
    <el-row type="flex">
      <el-col :offset="15">
        <div class="login-box">
          <div class="title">欢迎登录后台管理中心</div>
          <el-form ref="alignLogin" :label-position="'top'" :model="alignLogin" :rules="rules">
            <el-form-item
              label="账号"
              prop="user_name"
            ><el-input v-model="alignLogin.user_name" size="medium" placeholder="请输入账号" /></el-form-item>
            <el-form-item
              label="密码"
              prop="password"
            ><el-input v-model="alignLogin.password" size="medium" placeholder="请输入密码" show-password /></el-form-item>
            <el-form-item class="login-item-top">
              <el-button type="primary" @click.native.prevent="handleLogin">
                {{ loingLoading ? '登录中':'登录' }}
                <i v-if="loingLoading" class="el-icon-loading" />
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import SparkMD5 from 'spark-md5'

export default {
  data() {
    return {
      loingLoading: false,
      alignLogin: {
        user_name: '',
        password: ''
      },
      rules: {
        user_name: [
          {
            required: true,
            message: '请输入账号',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['blur', 'change']
          },
          {
            min: 6,
            message: '密码至少为6位',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  beforeCreate: function() {
    // 如果已经登录,自动跳转到首页
    if (this.$store.getters['login/isLogin']) {
      this.$router.push({ path: '/index' })
    }
  },
  created: function() {
    // 注册回车事件
    document.onkeydown = (e) => {
      var key = window.event.keyCode
      if (key == 13) {
        this.handleLogin()
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.alignLogin.validate((valid) => {
        if (valid) {
          this.loingLoading = true
          this.$http.post(
            '/admin/user/account/login',
            {
              ...this.alignLogin,
              password: SparkMD5.hash(this.alignLogin.password)
            },
            (res) => {
              this.$message.success(res.msg, () => {
                this.$store.dispatch('login/setLogin', res.data)
                this.$router.push({
                  path: '/index'
                })
              })
            },
            (error) => {
              this.disableSubmit = false
              this.loingLoading = false
              this.$message.error(error.msg)
            }
          )
        } else {
          this.loingLoading = false
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.admin-login {
  .el-input {
    input {
      border-top: none;
      border-left: none;
      border-right: none;
      border-radius: 0;
      padding: 0;
    }
  }

  .el-button {
    width: 100%;
    background-color: $button-color-primary;
    border-color: $button-color-primary;
  }
}
</style>
<style lang="scss" scoped>
.admin-login {
  width: 100%;
  height: 100%;
  background-image: url(../../assets/login/login-bg.png);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;

  .login-box {
    width: 380px;
    margin-top: 35%;
    padding: 10px;
    background-color: #ffffff;

    .title {
      font-size: 26px;
      color: $text-color-primary;
      margin-bottom: 40px;
    }

    .login-item-top {
      margin-top: 40px;
    }
  }
}
</style>
